import numpy as np
import calculateMat
import xlwt


class Solution:
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """

        def backtrack(first=0):
            # 所有数都填完了
            if first == n:
                res.append(nums[:])
            for i in range(first, n):
                # 动态维护数组
                nums[first], nums[i] = nums[i], nums[first]
                # 继续递归填下一个数
                backtrack(first + 1)
                # 撤销操作
                nums[first], nums[i] = nums[i], nums[first]

        n = len(nums)
        res = []
        backtrack()
        return res



if __name__ == '__main__':

    # 引入距离矩阵和重量向量

    point_height = []

    with open('height.txt', 'r', encoding='utf-8') as f:
        lines = f.readlines()  # 读取每一行数据
        # 处理每一行数据
        for line in lines:
            line = line.split('\n')[0]
            point_height.append(float(line))

    sum_height = np.array(point_height)
    print(sum_height.sum())

    # 距离矩阵
    _mat = calculateMat.calculate()

    f = xlwt.Workbook('encoding = utf-8')  # 设置工作簿编码
    sheet1 = f.add_sheet('sheet1', cell_overwrite_ok=True)  # 创建sheet工作表

    solution = Solution()
    height = [24.7, 20.9, 21.2, 17.6, 20.2, 22.0, 20.7, 23.2, 14.0]
    calculate_path = [
        [2, 6, 4, 5, 16],
        [17, 18, 24],
        [25, 19, 14],
        [20, 7, 13],
        [12, 11, 15],
        [27, 26],
        [28, 29, 30, 23],
        [21, 22, 10, 9, 8],
        [3, 1]
    ]
    for i in range(len(calculate_path)):
        ans = solution.permute(calculate_path[i])
        for k in range(len(ans)):
            tempPath = ans[k]
            outset = 0
            go_height = height[i]
            temp_go_price = 0
            temp_distance = 0
            price = 0  # 费用
            go_price = 0  # 去除费用
            back_price = 0  # 返程费用
            new_time = 0  # 时间
            for j in range(len(tempPath)):
                current_distance = _mat[outset, tempPath[j]]
                temp_distance += current_distance
                temp_go_price += current_distance * 3 * go_height
                go_height -= point_height[tempPath[j]]
                outset = tempPath[j]
            temp_new_time = 0.167 * len(tempPath) + temp_distance / 20 + _mat[0, outset] / 30
            temp_back_price = 2 * _mat[0, outset]
            temp_price = temp_go_price + temp_back_price
            price = temp_price
            go_price = temp_go_price
            back_price = temp_back_price
            new_time = temp_new_time

            sheet1.write(k+1, 5 * i + 1, str(price))  # 写入数据参数对应 行, 列, 值
            sheet1.write(k+1, 5 * i + 2, str(go_price))  # 写入数据参数对应 行, 列, 值
            sheet1.write(k+1, 5 * i + 3, str(back_price))  # 写入数据参数对应 行, 列, 值
            sheet1.write(k+1, 5 * i + 4, str(new_time))  # 写入数据参数对应 行, 列, 值
            sheet1.write(k+1, 5 * i, str(ans[k]))  # 写入数据参数对应 行, 列, 值

    f.save('全排列.xls')   # 保存.xls到当前工作目录
    print('ok')
